//
//  FindPasswordViewController.m
//  iStore
//
//  Created by MacDev on 15/4/16.
//  Copyright (c) 2015年 mirrtalk. All rights reserved.
//

#import "FindPasswordViewController.h"
#import "RequestEngine.h"
#import "NewLogViewController.h"

//正则表达式
#define kPhoneRegex @"^1[3|4|5|8][0-9]\\d{4,8}$"
#define kPassRefex  @"[1-9]\\d{5}(?!\\d)"

@interface FindPasswordViewController ()<UITextFieldDelegate>
{
    NSTimer *_timer;//定时器
    NSTimeInterval _seconds;//倒计时
    BOOL _once;//键盘次数
}
@end

@implementation FindPasswordViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"找回密码";
    _myPhone.delegate = self;
    _verificationCode.delegate = self;
    _myPhone.keyboardType = UIKeyboardTypeNumberPad;
    _verificationCode.keyboardType = UIKeyboardTypeNumberPad;
    
    
    [_VCode setBackgroundImage:[UIImage imageNamed:@"Rectangle_6"] forState:UIControlStateHighlighted];
    [_setPassword setBackgroundImage:[UIImage imageNamed:@"Rectangle_6"] forState:UIControlStateHighlighted];
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
   
}
- (IBAction)reSetButton:(UIButton *)sender{
   
    
    NSString *phone = _myPhone.text;
    NSString *passWord = _passWord.text;//密码
    NSString *rePassWord = _rePassWord.text;//重复密码
    NSString *verCode = _verificationCode.text;//验证码

    if ( passWord == nil || [passWord isEqualToString:@""] || rePassWord == nil || [rePassWord isEqualToString:@""] || verCode == nil || [verCode isEqualToString:@""] || _verificationCode.text == nil || [_verificationCode.text isEqualToString:@""]) {
        
        alert(@"内容不能为空，请填写完");
//        sender.backgroundColor = KUnSelectedColor;
        return;
    }
    if (![passWord isEqualToString:rePassWord]) {
        alert(@"两次输入的密码不同，请重新输入");
//        sender.backgroundColor = KUnSelectedColor;
        return;
    }
    
    //判断手机号
    if(_myPhone.text.length != 11){
        
        alert(@"您输入的手机号不符合规范，请重新输入");
//        sender.backgroundColor = KUnSelectedColor;
        return;
    }
    if (![self regexFlagWith:kPassRefex withParameter:verCode]) {
        alert(@"验证码不正确");
//        sender.backgroundColor = KUnSelectedColor;
        return;
    }
    //判断密码是否为6-18位
    if(_passWord.text.length < 6 || _passWord.text.length > 18){

        alert(@"密码不规范,请重新输入");
//        sender.backgroundColor = KUnSelectedColor;
        return;
    }
    
    [RequestEngine reSetPassWordWithDic:@{
                                                                    @"phone":phone,
                                                                                    @"validateCode":verCode,
                                                                                    @"newPassw":passWord
                                                                            }
                                                            completed:^(NSString *errorcode, NSDictionary *resultDict) {
                                          
                                          
                                                                //NSLog(@"<<<<<<<<<<<%@",resultDict);
                                                 if ([errorcode isEqualToString:@"0"]) {
                                          
                                                                //NSLog(@"_____________-%@",errorcode);
                                                                
                                                  alert(@"重置密码成功");
                                                                                [self.navigationController popViewControllerAnimated:YES];
                                                     

                                                     
                                                                     }
                                                  
                                              }];
    
    
}


//正则表达式的判断
- (BOOL) regexFlagWith:(NSString *)regex withParameter:(NSString *)parameter{
    
    NSPredicate *pass = [NSPredicate predicateWithFormat:@"SELF MATCHES %@",regex];
    
    return [pass evaluateWithObject:parameter];
    
}

- (IBAction)validateCodeButton:(UIButton *)sender {

//    sender.backgroundColor = KSelectedColor;

    
    if (_myPhone.text == nil || [_myPhone.text isEqualToString:@""]) {
        alert(@"手机号不能为空");
//        sender.backgroundColor = KUnSelectedColor;

        return;
    }
    if (_myPhone.text.length != 11) {
        alert(@"您输入的手机号不符合规范，请重新输入");
//        sender.backgroundColor = KUnSelectedColor;

        return;
    }
    [_returnButton setTitle:@"60秒" forState:UIControlStateNormal];
    _seconds = 60;
    _timer = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(countdown) userInfo:nil repeats:YES];
    NSString *myPhone = _myPhone.text;
    [RequestEngine verificationCodeWith:@{
                                          @"phone":myPhone,
                                          @"type":@"2",
                                          @"appKey":@"IOS"
                                          } completed:^(NSString *errorcode, NSDictionary *resultDict) {
                                              
            
                                          }];
}
- (void) countdown{
    
    _seconds--;
    NSString *time = [NSString stringWithFormat:@"%.0f秒",_seconds];
    [_returnButton setTitle:time forState:UIControlStateNormal];
    [_returnButton setEnabled:NO];
    
    if (_seconds==0) {
        [_timer invalidate];
        [_returnButton setTitle:@"领取验证码" forState:UIControlStateNormal];
        [_returnButton setEnabled:YES];
    }
    
    
}
#pragma mark --输入框上移--
- (IBAction)textFiledBegin:(id)sender {
    if (!_once) {
        [UIView animateWithDuration:0.3 animations:^{
            CGRect frame = self.view.frame;
            frame.origin.y  = -100;
            self.view.frame = frame;
        }];
        _once = true;
    }
    
}

// 取消键盘
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    [self.view endEditing:YES];
    [self testFileDidFinish];
    _once = false;
}


- (void)textFieldDidEndEditing:(UITextField *)textField
{
    [self testFileDidFinish];
    [UIView animateWithDuration:0.3 animations:^{
        CGRect frame = self.view.frame;
        frame.origin.y  -= 100;
        self.view.frame = frame;
    }];
    
    
}

- (void)testFileDidFinish
{
    [UIView animateWithDuration:0.3 animations:^{
        CGRect frame =self.view.frame;
        frame.origin.y = 0.0;
        self.view.frame = frame;
    }];
    [self.view endEditing:YES];
    
}
@end
